<template>
  <div class="gi_table_page">
    <GiTable
      title="任务日志"
      row-key="id"
      :data="dataList"
      :columns="columns"
      :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1300 }"
      :pagination="pagination"
      :disabled-tools="[]"
      @refresh="search"
    >
      <template #toolbar-left>
        <a-input-search v-model="queryForm.title" placeholder="搜索任务名称" allow-clear @search="search" />
        <a-select
          v-model="queryForm.result"
          placeholder="请选择结果"
          :options="job_result_enum"
          allow-clear
          style="width: 150px"
          @change="search"
        />
        <DateRangePicker v-model="queryForm.datetimeRange" :allow-clear="false" @change="search" />
        <a-button @click="reset">
          <template #icon><icon-refresh /></template>
          <template #default>重置</template>
        </a-button>
      </template>
      <template #result="{ record }">
        <GiCellTag :value="record.result" :dict="job_result_enum" />
      </template>
      <template #duration="{ record }">
        <a-tag v-if="record.duration > 500" color="red">{{ record.duration }}ms</a-tag>
        <a-tag v-else-if="record.duration > 200" color="orange">{{ record.duration }}ms</a-tag>
        <a-tag v-else color="green">{{ record.duration }} ms</a-tag>
      </template>
      <template #action="{ record }">
        <a-space>
          <a-button v-permission="['scheduler:log:view']" size="mini" title="详情" @click="onDetail(record)">详情</a-button>
        </a-space>
      </template>
    </GiTable>

    <LogDetailDrawer ref="LogDetailDrawerRef" />
  </div>
</template>

<script setup lang="ts">
import type { TableInstance } from '@arco-design/web-vue'
import { useRoute } from 'vue-router'
import dayjs from 'dayjs'
import LogDetailDrawer from './LogDetailDrawer.vue'
import { type JobLogQuery, type JobLogResp, pageJobLog } from '@/apis/scheduler'
import { useTable } from '@/hooks'
import { useDict } from '@/hooks/app'
import { isMobile } from '@/utils'
import has from '@/utils/has'

defineOptions({ name: 'SchedulerLog' })

const { job_result_enum } = useDict('job_result_enum')

const queryForm = reactive<JobLogQuery>({
  datetimeRange: [
    dayjs().subtract(6, 'day').startOf('day').format('YYYY-MM-DD HH:mm:ss'),
    dayjs().endOf('day').format('YYYY-MM-DD HH:mm:ss')
  ]
})

const {
  tableData: dataList,
  pagination,
  loading,
  search
} = useTable((page) => pageJobLog({ ...queryForm, ...page }), { immediate: false })
const columns: TableInstance[] = [
  {
    title: '序号',
    width: 66,
    align: 'center',
    render: ({ rowIndex }) => h('span', {}, rowIndex + 1 + (pagination.current - 1) * pagination.pageSize)
  },
  { title: '任务名称', dataIndex: 'title', width: 100, ellipsis: true, tooltip: true },
  { title: '耗时', dataIndex: 'duration', slotName: 'duration', width: 180, align: 'center' },
  { title: '执行结果', dataIndex: 'result', slotName: 'result', width: 180, align: 'center' },
  { title: '创建时间', dataIndex: 'createTime', width: 180 },
  {
    title: '操作',
    slotName: 'action',
    width: 150,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
    show: has.hasPermOr(['scheduler:log:view'])
  }
]

// 重置
const reset = () => {
  queryForm.result = undefined
  queryForm.datetimeRange = [
    dayjs().subtract(6, 'day').startOf('day').format('YYYY-MM-DD HH:mm:ss'),
    dayjs().endOf('day').format('YYYY-MM-DD HH:mm:ss')
  ]
  search()
}

const LogDetailDrawerRef = ref<InstanceType<typeof LogDetailDrawer>>()
// 详情
const onDetail = (record: JobLogResp) => {
  LogDetailDrawerRef.value?.onOpen(record)
}

const route = useRoute()
onMounted(() => {
  if (route.query) {
    queryForm.jobId = route.query.jobId ? Number.parseInt(route.query.jobId as string, 10) : undefined
    queryForm.title = route.query.title ? route.query.title : undefined
  }
  search()
})
</script>

<style scoped lang="scss"></style>
